package com.asia5b.a5mall.goods.adapter

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.asia5b.a5mall.goods.R
import com.asia5b.a5mall.middleware.model.goods.SelectorBean
import com.asia5b.a5mall.middleware.model.goods.SelectorKeyBean
import com.asia5b.a5mall.middleware.utils.DisplayUtils
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.middleware.widget.GridSpacingItemDecoration
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/5/13.
 */
class GoodsSelectorKeyAdapter(list: ArrayList<SelectorKeyBean>, var params: HashMap<String, Any>) :
        BaseQuickAdapter<SelectorKeyBean, BaseViewHolder>(R.layout.item_goodsselector_key_adapter, list) {
    override fun convert(helper: BaseViewHolder, item: SelectorKeyBean) {
        //是否显示分割线
        helper.setGone(R.id.view_goodsselector_divider_bottom, !item.isShowDividerBottom)
        //显示值
        helper.setText(R.id.tv_goodsselector_key, item.key)
        //右边箭头
        val iv_goodsselector_arrow = helper.getView<ImageView>(R.id.iv_goodsselector_arrow)
        //选中的值
        val tv_goodsselector_selectortext = helper.getView<TextView>(R.id.tv_goodsselector_selectortext)
//        tv_goodsselector_selectortext.setText(SearchGoodsHelper.getSelectValue(item, params))
        tv_goodsselector_selectortext.setText(item.selectItem?.name)
        //值列表
        val recyclerView = helper.getView<RecyclerView>(R.id.rv_goodsselector_value)
        val valueList = item.value!!
        //有值列表才显示
        recyclerView.visibility = (valueList.size == 0).judge(View.GONE, View.VISIBLE)
        //值列表>3才显示箭头
        iv_goodsselector_arrow.visibility = (valueList.size <= 3).judge(View.GONE, View.VISIBLE)
        //值列表>3 显示收缩
        iv_goodsselector_arrow.isSelected = item.isSingleLine
        if (valueList.size > 0) {
            val mAdapter = GoodsSelectorValueAdapter(item, valueList, tv_goodsselector_selectortext)
            recyclerView.run {
                layoutManager = GridLayoutManager(context, 3)
                adapter = mAdapter
                if (itemDecorationCount == 0) {
                    addItemDecoration(GridSpacingItemDecoration(3, DisplayUtils.dip2px(context, 5f), false))
                }
            }
            mAdapter.setOnItemClickListener { adapter, view, position ->
                val selectSelectorBean = adapter.getItem(position) as SelectorBean
                if (item.selectItem == selectSelectorBean) {
                    item.selectItem = null
                }else{
                    item.selectItem=selectSelectorBean
                }
                mAdapter.notifyDataSetChanged()
                notifyDataSetChanged()
            }
            if (valueList.size > 3) {
                tv_goodsselector_selectortext.setOnClickListener {
                    item.isSingleLine = !item.isSingleLine
                    mAdapter.notifyDataSetChanged()
                    notifyDataSetChanged()
                }
            }
        }

    }
}